package com.hxg.common.service.impl;

import cn.hutool.core.util.ReflectUtil;
import com.hxg.common.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.bouncycastle.crypto.tls.MACAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@Service("commonService")
public class CommonServiceImpl implements CommonService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(this.jdbcTemplate);
    }

    @Override
    public void dealOrderNum(Object newObj, Object oldObj, String tableName) {
        Long orderNum = Long.parseLong(ReflectUtil.invoke(newObj, "getOrderNum").toString());
        Long id = Long.parseLong(ReflectUtil.invoke(newObj, "getId").toString());

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orderNum", orderNum);
        paramMap.put("id", id);
        String updateSQL = String.format("update %s set order_num = order_num + 1 where order_num > :orderNum",
                tableName);
        log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
        this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        updateSQL = String.format("update %s set order_num = order_num - 1 where order_num < :orderNum",
                tableName);
        log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
        this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);

        Long oldOrderNum = Long.parseLong(ReflectUtil.invoke(oldObj, "getOrderNum").toString());
        if (orderNum > oldOrderNum) {
            updateSQL = String.format("update %s set order_num = order_num - 1 where order_num = :orderNum", tableName);
            log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
            this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        } else {
            updateSQL = String.format("update %s set order_num = order_num + 1 where order_num = :orderNum", tableName);
            log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
            this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        }
    }

    @Override
    public void reSortOrderNum(String tableName) {
        String updateSQL = String.format("update %s a, (select @rownum := @rownum + 1 rownum, id\n" +
                "                    from (select @rownum := 0) r,\n" +
                "                         %s\n" +
                "                    order by order_num) b\n" +
                "        set a.order_num = b.rownum\n" +
                "        where a.id = b.id", tableName, tableName);
        this.jdbcTemplate.update(updateSQL);
    }

    @Override
    public void dealOrderNum(Object newObj, Object oldObj, String tableName, List<Map<String, Object>> colValList) {
        Map<String, Object> paramMap = new HashMap<>();
        String tmpSQL = "";
        if (CollectionUtils.isNotEmpty(colValList)) {
            for (Map<String, Object> map : colValList) {
                for (String key : map.keySet()) {
                    paramMap.put(key, MapUtils.getString(map, key, ""));
                    tmpSQL += String.format(" and %s = %s", key, MapUtils.getString(map, key, ""));
                }
            }
        }

        Long orderNum = Long.parseLong(ReflectUtil.invoke(newObj, "getOrderNum").toString());
        Long id = Long.parseLong(ReflectUtil.invoke(newObj, "getId").toString());

        paramMap.put("orderNum", orderNum);
        paramMap.put("id", id);
        String updateSQL = String.format("update %s set order_num = order_num + 1 where order_num > :orderNum",
                tableName) + tmpSQL;
        log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
        this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        updateSQL = String.format("update %s set order_num = order_num - 1 where order_num < :orderNum",
                tableName) + tmpSQL;
        log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
        this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);

        Long oldOrderNum = Long.parseLong(ReflectUtil.invoke(oldObj, "getOrderNum").toString());
        if (orderNum > oldOrderNum) {
            updateSQL = String.format("update %s set order_num = order_num - 1 where order_num = :orderNum", tableName) + tmpSQL;
            log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
            this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        } else {
            updateSQL = String.format("update %s set order_num = order_num + 1 where order_num = :orderNum", tableName) + tmpSQL;
            log.info("dealOrderNum:\n{}\n参数: {}", updateSQL, paramMap);
            this.getNamedParameterJdbcTemplate().update(updateSQL, paramMap);
        }
    }

    @Override
    public void reSortOrderNum(String tableName, List<Map<String, Object>> colValList) {
        Map<String, Object> paramMap = new HashMap<>();
        String tmpSQL = "update %s a, (select @rownum := @rownum + 1 rownum, id\n" +
                "                    from (select @rownum := 0) r,\n" +
                "                         %s where 1 = 1\n";
        if (CollectionUtils.isNotEmpty(colValList)) {
            for (Map<String, Object> map : colValList) {
                for (String key : map.keySet()) {
                    paramMap.put(key, MapUtils.getString(map, key, ""));
                    tmpSQL += String.format(" and %s = %s", key, MapUtils.getString(map, key, ""));
                }
            }
        }
        tmpSQL += "                    order by order_num) b\n" +
                "        set a.order_num = b.rownum\n" +
                "        where a.id = b.id";
        String updateSQL = String.format(tmpSQL, tableName, tableName);
        this.jdbcTemplate.update(updateSQL);
    }
}
